package org.hxyjs.dao.impl;

import org.hxyjs.dao.MatchTypeDao;
import org.hxyjs.entity.MatchType;
import org.hxyjs.mapper.MatchTypeMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("matchTypeDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class MatchTypeDaoImpl implements MatchTypeDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private MatchTypeMapper matchTypeMapper;

    public List<MatchType> getAllMatchTypes() throws Exception {
        return matchTypeMapper.selectByExample(null);
    }

    @Override
    public List<MatchType> getMatchTypeByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) matchTypeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return matchTypeMapper.getMatchTypeByPage(map);
    }

//    @Override
//    public MatchType selectMatchTypeByMatno(String sno) throws Exception {
//        return matchTypeMapper.selectByPrimaryKey(sno);
//    }


    @Override
    public Integer deleteMatchTypeByMatno(String matno) {
        return matchTypeMapper.deleteByPrimaryKey(matno);
    }

    @Override
    public Integer updateMatchTypeByMatno(MatchType matchType) {
        return matchTypeMapper.updateByPrimaryKey(matchType);
    }

    @Override
    public Integer addMatchType(MatchType matchType) {
        matchType.setMatno(initId());
        return matchTypeMapper.insertSelective(matchType);
    }
//
//    @Override
//    public MatchType selectMatchTypeWithorderAndUser(String matno) throws Exception {
//        return matchTypeMapper.selectMatchTypeWithorderAndUser(matno);
//    }

    public String initId() {
        String maxMatno = matchTypeMapper.getMaxMatno();
        return GenNumTools.initId("m", 3, maxMatno);
    }

    @Override
    public MatchType getMatchTypeByMatno(String matno) throws Exception {
        return matchTypeMapper.selectByPrimaryKey(matno);
    }

}